package UI.Windows
{
	import Events.*;
	
	import UI.Buttons.*;
	import UI.LessonPlanViewer.*;
	import UI.List.*;
	import UI.Loader.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	import mx.core.UIComponent;
	
	import spark.components.Window;
	
	public class UIAddTaskWindow extends Window
	{		
		private const TEXT_HEIGHT:Number=20;
		private const TEXT_WIDTH:Number=100;
		
		private var taskName:TextField;
		private var taskDescription:TextField;
		private var taskNameInput:TextField;
		private var taskDescriptionInput:TextField;
		private var submitButton:UIButton;
		
		public function UIAddTaskWindow(classAssets:Object)
		{
			super();
			
			submitButton = new UIButton(classAssets.SubmitButton);
			taskName = generateTextField("Task Name",false);
			taskDescription=generateTextField("Task Description",false);
			taskNameInput=generateTextField("",true);
			taskDescriptionInput=generateTextField("",true);	
			
			addEventListeners();
			addElements();
			positionElements();
		}
		
		private function generateTextField(text:String,isInput:Boolean):TextField
		{
			var tf:TextFormat=new TextFormat(null,null,null,!isInput);
			tf.align = TextFormatAlign.CENTER;
			var field:TextField = new TextField();
			field.text = text;
			field.height=TEXT_HEIGHT;
			field.width = TEXT_WIDTH;
			field.selectable=isInput;
			field.mouseEnabled=isInput;
			if(isInput)
				field.type = TextFieldType.INPUT;
			field.setTextFormat(tf);
			if(isInput)
			{
				field.background=true;
				field.backgroundColor = uint(0xd9d9d9);
			}
			return field;
		}
		
		public function positionElements():void
		{
			taskName.x = this.width/2-taskName.width/2;
			taskNameInput.x = this.width/2-taskNameInput.width/2;
			taskNameInput.y = taskName.height+5;
			
			taskDescription.x = this.width/2-taskDescription.width/2;
			taskDescription.y=taskNameInput.y+taskNameInput.height+10;
			taskDescriptionInput.x = this.width/2-taskDescriptionInput.width/2;
			taskDescriptionInput.y = taskDescription.y+taskDescription.height;
			
			submitButton.x = this.width/2-submitButton.width/2;
			submitButton.y=taskDescriptionInput.y+taskDescriptionInput.height+10;
		}
		
		public override function open(openWindowActive:Boolean=true):void
		{
			super.open(openWindowActive);
			positionElements();
			this.width = 
				Math.max(taskName.x+taskName.width,taskNameInput.x+taskNameInput.width,taskDescription.x+
					taskDescription.width,taskDescriptionInput.x+taskDescriptionInput.width,submitButton.x+submitButton.width)
					+(nativeWindow.width-nativeWindow.stage.stageWidth)+20;
			this.height = submitButton.height+submitButton.y+(nativeWindow.height-nativeWindow.stage.stageHeight);
		}
		
		public override function set width(w:Number):void
		{
			super.width=w;
			taskName.width=w-20;
			taskDescription.width=w-20;
			taskNameInput.width=w-20;
			taskDescriptionInput.width=w-20;
			positionElements();
		}
		
		public override function set height(h:Number):void
		{
			super.height=h;
			positionElements();
		}
		
		private function addEventListeners():void
		{
			submitButton.addEventListener(MouseEvent.CLICK,submitButtonClicked);
		}
		
		private function addElements():void
		{
			var wrapper:UIComponent = new UIComponent();
			addElement(submitButton);
			addElement(wrapper);
			wrapper.addChild(taskName);
			wrapper.addChild(taskDescription);
			wrapper.addChild(taskNameInput);
			wrapper.addChild(taskDescriptionInput);
		}
		
		private function submitButtonClicked(e:MouseEvent):void
		{
			dispatchEvent(new UIAddTaskWindowEvent(UIAddTaskWindowEvent.SUBMIT,taskNameInput.text,taskDescriptionInput.text));
		}
	}
}